<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
use Think\Exception;

class OrderController extends AdminbaseController{
	
	public function _initialize() {
		parent::_initialize();
	}

    //订单列表
    public function index(){
        $sn=I('sn');
        $phone=I('phone');
        $name=I('name');
        $start_time = strtotime(I('request.start_time'));
        $end_time = strtotime(I('request.end_time'));
        //99为全部订单
        $status=I('status',99,'intval');
        if($sn){
            $where['o.sn']=$sn;
        }
        if($phone){
            $where['u.phone']=$phone;
        }
        if($name){
            $where['b.name']=$name;
        }
        if (!empty($start_time)) {
            $where['o.create_time'] = array(
                array('EGT', $start_time)
            );
        }
        if (!empty($end_time)) {
            if (empty($where['o.create_time'])) {
                $where['o.create_time'] = array();
            }
            array_push($where['o.create_time'], array('ELT', $end_time));
        }
        if($status!=99){
            $where['o.status']=$status;
        }
        $count=M('Order')
            ->alias('o')
            ->join('LEFT JOIN __BUSINESS_SERVICE__ as bs ON bs.id=o.business_service_id')
            ->join('LEFT JOIN __BUSINESS__ as b ON b.id=bs.business_id')
            ->join('LEFT JOIN __USER__ as u ON u.id=o.user_id')
            ->where($where)
            ->count();
        $page = $this->page($count, 20);

        $orders=M('Order')
            ->alias('o')
            ->join('LEFT JOIN __BUSINESS_SERVICE__ as bs ON bs.id=o.business_service_id')
            ->join('LEFT JOIN __BUSINESS__ as b ON b.id=bs.business_id')
            ->join('LEFT JOIN __USER__ as u ON u.id=o.user_id')
            ->where($where)
            ->order('o.create_time DESC')
            ->limit($page->firstRow, $page->listRows)
            ->field('o.sn,o.id,o.business_service_name,o.total,b.name,o.status,o.is_complained,u.username,u.phone,o.payment,o.create_time')
            ->select();

        $total=M('Order')->where(['status'=>['neq',0]])->sum('total');
        $this->assign('orders',$orders);
        $this->assign('total',$total);
        $this->assign("page", $page->show('Admin'));
        $this->assign('start_time', empty($start_time) ? '' : $start_time);
        $this->assign('end_time', empty($end_time) ? '' : $end_time);
        $this->display();
    }

    //活动订单列表
    public function activity(){
        $sn=I('sn');
        $phone=I('phone');
        $name=I('name');
        $start_time = strtotime(I('request.start_time'));
        $end_time = strtotime(I('request.end_time'));
        //99为全部订单
        $status=I('status',99,'intval');
        if($sn){
            $where['o.sn']=$sn;
        }
        if($phone){
            $where['u.phone']=$phone;
        }
        if($name){
            $where['b.name']=$name;
        }
        if (!empty($start_time)) {
            $where['o.create_time'] = array(
                array('EGT', $start_time)
            );
        }
        if (!empty($end_time)) {
            if (empty($where['o.create_time'])) {
                $where['o.create_time'] = array();
            }
            array_push($where['o.create_time'], array('ELT', $end_time));
        }
        if($status!=99){
            $where['o.status']=$status;
        }
        $count=M('OrderActivity')
            ->alias('o')
            ->join('LEFT JOIN __BUSINESS_ACTIVITY__ as ba ON ba.id=o.business_activity_id')
            ->join('LEFT JOIN __BUSINESS__ as b ON b.id=ba.business_id')
            ->join('LEFT JOIN __USER__ as u ON u.id=o.user_id')
            ->where($where)
            ->count();
        $page = $this->page($count, 20);

        $orders=M('OrderActivity')
            ->alias('o')
            ->join('LEFT JOIN __BUSINESS_ACTIVITY__ as ba ON ba.id=o.business_activity_id')
            ->join('LEFT JOIN __BUSINESS__ as b ON b.id=ba.business_id')
            ->join('LEFT JOIN __USER__ as u ON u.id=o.user_id')
            ->where($where)
            ->order('o.create_time DESC')
            ->limit($page->firstRow, $page->listRows)
            ->field('o.sn,o.id,ba.name as business_activity_name,o.total,b.name,o.status,o.is_complained,u.username,u.phone,o.payment,o.create_time,ba.start_time as st,ba.end_time as et')
            ->select();

        $total=M('OrderActivity')->where(['status'=>['neq',0]])->sum('total');
        $this->assign('orders',$orders);
        $this->assign('total',$total);
        $this->assign("page", $page->show('Admin'));
        $this->assign('start_time', empty($start_time) ? '' : $start_time);
        $this->assign('end_time', empty($end_time) ? '' : $end_time);
        $this->display();
    }
}